using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;
 
namespace Grebok.Domain
{
    /// <summary>
    /// Defines a customer
    /// </summary>
    public class Customer : DomainObject
    {
        /// <summary>
        /// Creates a new customer
        /// </summary>
        /// <param name="id">The customer Id</param>
        /// <param name="companyName">The company name</param>
        /// <param name="contactName">The contact name</param>
        /// <returns>The customer</returns>
        public static Customer CreateNewCustomer(string id, string companyName, string contactName)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id", "Customer Id cannot be null");
            }
            if (companyName == null)
            {
                throw new ArgumentNullException("companyName", "Company Name cannot be null");
            }
            if (contactName == null)
            {
                throw new ArgumentNullException("contactName", "Contact Name cannot be null");
            }
            Customer c = new Customer() { Id=id, CompanyName=companyName };
            c.ContactInfo = new ContactInfo { ContactName = contactName };
            return c;
        }

        /// <summary>
        /// Creates a customer instance
        /// </summary>
        public Customer()
        {
            this.Id = string.Empty;
            this.CompanyName = string.Empty;
            this.PhoneNumber = string.Empty;
            this.FaxNumber = string.Empty;
            this.AddressInfo = new AddressInfo();
            this.ContactInfo = new ContactInfo();
            this.Orders = new List<Order>();
        }

        /// <summary>
        /// Gets or sets the Id
        /// </summary>
        public virtual string Id { get; set; }

        /// <summary>
        /// Gets or sets the Company Name
        /// </summary>
        public virtual string CompanyName { get; set; }

        /// <summary>
        /// Gets or sets the phone number
        /// </summary>
        public virtual string PhoneNumber { get; set; }

        /// <summary>
        /// Gets or sets the fax number
        /// </summary>
        public virtual string FaxNumber { get; set; }

        /// <summary>
        /// Gets or sets the address informations
        /// </summary>
        public virtual AddressInfo AddressInfo { get; set; }

        /// <summary>
        /// gets or sets the contact informations
        /// </summary>
        public virtual ContactInfo ContactInfo { get; set; }

        /// <summary>
        /// Gets the customer's orders
        /// </summary>
        public virtual IList<Order> Orders { get; set; }

        /// <summary>
        /// Calculates the income generated by the customer
        /// </summary>
        /// <returns>The total income</returns>
        public virtual decimal CalculateTotalIncome()
        {
            decimal income = 0;
            foreach (Order order in this.Orders)
            {
                income += order.CalculatePrice();
            }
            return income;
        }



        /// <summary>
        /// Returns whether the customer is valid for registration
        /// </summary>
        public virtual bool IsValidForRegistration
        {
            get
            {
                ValidationResults results = ValidateForRegistration();
                return results.IsValid;
            }
        }

        /// <summary>
        /// Returns validation info for new customer registation
        /// </summary>
        /// <returns>The validation informations</returns>
        public virtual ValidationResults ValidateForRegistration()
        {
            Validator validator = ValidationFactory.CreateValidator<Customer>("IsValidForRegistration");
            ValidationResults results = validator.Validate(this);
            return results;
        }

        /// <summary>
        /// Returns whether the customer can make orders
        /// </summary>
        public virtual bool CanMakeOrders
        {
            get
            {
                Validator validator = ValidationFactory.CreateValidator<Customer>("CanMakeOrders");
                ValidationResults results = validator.Validate(this);

                return results.IsValid && IsValidForRegistration;
            }
        }
    }
}
